package com.sticktogether;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.android.gms.maps.model.Marker;
import com.sticktogether.server.ServerCommunication;
import com.sticktogether.usermanagement.Member;

/**
 * This activity shows a list of all persons of the current session
 * 
 * @author David Schattel
 * @author Fabian Stark
 * @since 20.12.12
 */
public class SessionActivity extends ListActivity {

    //DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW
    private ArrayAdapter<String> adapter;
	private LinkedList<String> 	mListOptions;
	private ListView 			memberList;
    private int					mNumberOfMembers;
    
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
    	// remove title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // set FullScreen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        setContentView(R.layout.activity_session);
        
        memberList = (ListView) findViewById(android.R.id.list);
                
        memberList.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				if(position < mNumberOfMembers) {
					ServerCommunication.showInfoWindow = 1;
					String name = (String)((TextView)arg1).getText();
					name = name.split("\\(")[0].trim();
					
					ServerCommunication.map.showInfoWindowOfThisMember = ServerCommunication.usernameToUserid.get(name);
					finish();
				}
				else if(position > mNumberOfMembers) {
					ServerCommunication.showInfoWindow = 2;
					String name = (String)((TextView)arg1).getText();
					ServerCommunication.map.showInfoWindowOfThisMarker = name;
					finish();
				}
			}
        });
        		        
        loadMembers();
        
        //adapter can listen to changes in the list and apply it on the list view
        adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mListOptions);
        setListAdapter(adapter);
        
    }
    
    /**
     * Load the list of members from the shared preferences and add them to the listview
     */
    private void loadMembers(){
    	mListOptions = new LinkedList<String>();
		// Get names of all members
    	Iterator<Map.Entry<Integer, Member>> it = ServerCommunication.members.entrySet().iterator();
		while (it.hasNext()) {
			Member m = it.next().getValue();
			mListOptions.add(m.getUsername() + " (" + m.getLastUpdateTime()+ ")");
		}
		mNumberOfMembers  = mListOptions.size();
		Collections.sort(mListOptions);
		
		// do we also have markers?
		if(!ServerCommunication.pins.isEmpty()) {
		
			// ok, add a separator now
			mListOptions.add("---- Marker ----");
			
			// and now let's get all markers
			LinkedList<String> Markers = new LinkedList<String>();
			Iterator<Entry<String, Marker>> it2 = ServerCommunication.pins.entrySet().iterator();
			while (it2.hasNext()) {
				Markers.add(it2.next().getValue().getTitle());
			}
			Collections.sort(Markers);
			
			// add the markers behind the users and separator
			mListOptions.addAll(Markers);
		}
		
    }

    
}